<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-05-19 10:08:11
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-07-06 15:15:56
 */

namespace Addons\CentralControl\Services;

use Addons\CentralControl\Models\CentralControlModel;
use Common\BaseService;
use Common\Helpers\DebugHelper;
use Common\Models\Enums\DeviceTypeEnums;
use Common\Services\Sys\DeviceService;


/**
 * 控制面板数据操作
 * @date 2023-05-19
 * @example
 * @author Wang Chunsheng
 * @since
 */
class CentralService extends BaseService
{
    /**
     * 设备数据初始化存储
     * @param int $project_id
     * @param string $clientid
     * @param string $name
     * @return array
     * @date 2023-05-19
     * @example
     * @author Wang Chunsheng
     * @since
     */
    public static function saveDeviced(int $project_id,string $clientid, string $name): array
    {
        $device_type = DeviceTypeEnums::DeviceType01;
        $deviceIdUpper = $clientid;
        $CentralControlModel = new CentralControlModel();
        $isHave = $CentralControlModel->query()->where(['clientid' => $clientid])->value('id');
        if (!$isHave && $deviceIdUpper) {
            $device_id = DeviceService::initDevice($deviceIdUpper, 'CentralControl', $device_type);
            $CentralControlModel->addInfo([
                'device_id' => $device_id,
                'clientid' => $clientid,
                'device_type' => $device_type,
                'project_id' => $project_id,
                'name' => $name,
            ]);
            return [
                'device_id' => $device_id,
                'clientid' => $clientid,
                'device_type' => $device_type,
                'project_id' => $project_id,
                'name' => $name
            ];
        }else{
            return $CentralControlModel->query()->where(['clientid' => $clientid])->first()->toArray();
        }

    }

    /**
     * 更新设备fd
     */
    public static function upDevicedFd(int $websocket_fd, string $clientid): bool
    {
        $CentralControlModel = new CentralControlModel();
        return $CentralControlModel->updateInfo(['websocket_fd' => $websocket_fd], ['clientid' => $clientid]);
    }

    /**
     * 更新设备状态数据
     * @param [type] $mac
     * @param array $data
     * @return bool
     * @date 2023-05-19
     * @example
     * @author Wang Chunsheng
     * @since
     */
    public static function upDeviced($clientid, array $data = []): bool
    {
        $deviceIdUpper = $clientid;
        $CentralControlModel = new CentralControlModel();
        $isHave = $CentralControlModel->query()->where('clientid' , $deviceIdUpper)->value('id');

        DebugHelper::consoleWrite('更新设备状态数据-开关数据是否存在校验', [
            'isHave' => $isHave,
            'clientid' => $deviceIdUpper,
            'DATA'=>$data
        ]);

        if ($isHave) {
          return  $CentralControlModel->updateInfo($data, ['clientid' => $deviceIdUpper]);
        }
        return false;
    }

    /**
     * 获取设备信息
     * @param int $device_id
     * @param array $column
     * @return array
     * @date 2023-07-06
     * @example
     * @author Wang Chunsheng
     * @since
     */
    public static function getDevicedByDid(int $device_id, array $column= ['*']): array
    {
        $CentralControlModel = new CentralControlModel();
        $Device = $CentralControlModel->query()->where(['device_id' => $device_id])->first($column);
        return $Device?$Device->toArray():[];
    }

    static function getClientidByCode($sys_code)
    {
        $CentralControlModel = new CentralControlModel();
        return $CentralControlModel->query()->where(['sys_code' => $sys_code])->value('clientid');
    }

    public static function getDevicedByClientId(string $clientid,$columns = ['*']): array
    {
        $CentralControlModel = new CentralControlModel();
        return $CentralControlModel->query()->where(['clientid' => $clientid])->first($columns)->toArray();
    }

    public static function addDeviceReStartLog($mac, $wifiMac, $power_on_num, $project_id = 0): bool
    {
        $DeviceReStartLog = new DeviceReStartLog();
        return $DeviceReStartLog->addInfo([
            'mac' => $mac,
            'wifiMac' => $wifiMac,
            'power_on_num' => $power_on_num,
            'project_id' => $project_id
        ]);
    }

    /**
     * 更新字段device_obj，他为json类型,需要可以修改其中的字段
     */
    public static function upDeviceObj(string $clientid, array $data): bool
    {
        $CentralControlModel = new CentralControlModel();
        return $CentralControlModel->updateInfo(['device_obj' => json_encode($data)], ['clientid' => $clientid]);
    }
}
